//////////////////////////////////////////////////////////////////////////
#include "Config/InitializerList.hpp"
#include "Config/Config.hpp"
#include <assert.h>


//////////////////////////////////////////////////////////////////////////
Config::InitializerList*		Config::InitializerList::mpFirst	=	nullptr;


//////////////////////////////////////////////////////////////////////////
Config::InitializerList::~InitializerList()
{
	// Store value if the config system is enabled.
	if( mpPrev )		mpPrev->mpNext	=	mpNext;
	if( mpNext )		mpNext->mpPrev	=	mpPrev;
	if( mpFirst==this )	mpFirst			=	mpNext;

	// Store if possible.
	_Store();

	delete mpItem;
}


//////////////////////////////////////////////////////////////////////////
Config::InitializerList* Config::InitializerList::Prev() const
{
	return mpPrev;
}


Config::InitializerList* Config::InitializerList::Next() const
{
	return mpNext;
}


Config::Item* Config::InitializerList::Item() const
{
	return mpItem;
}


//////////////////////////////////////////////////////////////////////////
Config::InitializerList* Config::InitializerList::First()
{
	return mpFirst;
}


Config::Item* Config::InitializerList::Find( const std::string& keyName )
{
	InitializerList*	current	=	mpFirst;
	while( current )
	{
		if( current->Item()->Key()==keyName )
			break;
		current	=	current->Next();
	}

	if( current )
		return current->Item();
	return nullptr;
}


//////////////////////////////////////////////////////////////////////////
void Config::InitializerList::_Initialize()
{
	if( Config::Instance )
		Config::Instance->Initialize( *mpItem );
}


void Config::InitializerList::_Store()
{
	if( Config::Instance )
		Config::Instance->Store( *mpItem );
}
